<template>
  <view 
    v-if="show"
    class="fu-snackbar"
    :class="[
      `fu-snackbar--${position}`,
      `fu-snackbar--${type}`,
      { 'fu-snackbar--with-action': action }
    ]"
  >
    <!-- 内容 -->
    <text class="fu-snackbar__message">{{ message }}</text>
    
    <!-- 操作按钮 -->
    <view 
      v-if="action"
      class="fu-snackbar__action"
      @click="onAction"
    >{{ action }}</view>
  </view>
</template>

<script>
/**
 * fu-snackbar 消息条组件
 * @description 在屏幕底部或顶部显示的消息提示，类似于 Android 的 Snackbar
 * @property {Boolean} show - 是否显示
 * @property {String} message - 文本内容
 * @property {String} action - 操作按钮文字，不设置则不显示
 * @property {String} type - 主题类型，可选值为 default primary success warning error
 * @property {String} position - 位置，可选值为 top bottom
 * @property {Number} duration - 展示时长(ms)，值为 0 时不会自动关闭
 * @event {Function} action - 点击操作按钮时触发
 * @event {Function} close - 关闭时触发
 */
export default {
  name: 'fu-snack-bar',
  props: {
    show: {
      type: Boolean,
      default: false
    },
    message: {
      type: String,
      default: ''
    },
    action: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      default: 'default',
      validator: value => ['default', 'primary', 'success', 'warning', 'error'].includes(value)
    },
    position: {
      type: String,
      default: 'bottom',
      validator: value => ['top', 'bottom'].includes(value)
    },
    duration: {
      type: Number,
      default: 4000
    }
  },
  emits: ['update:show', 'action', 'close'],
  data() {
    return {
      timer: null
    }
  },
  watch: {
    show(val) {
      if (val) {
        this.clearTimer()
        if (this.duration > 0) {
          this.timer = setTimeout(() => {
            this.onClose()
          }, this.duration)
        }
      }
    }
  },
  beforeUnmount() {
    this.clearTimer()
  },
  methods: {
    clearTimer() {
      if (this.timer) {
        clearTimeout(this.timer)
        this.timer = null
      }
    },
    onAction() {
      this.$emit('action')
      this.onClose()
    },
    onClose() {
      this.clearTimer()
      this.$emit('update:show', false)
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss" scoped>
.fu-snackbar {
  position: fixed;
  left: 0;
  right: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 28rpx 32rpx;
  font-size: 28rpx;
  line-height: 1.5;
  
  &--default {
    background-color: #323232;
    color: #fff;
    
    .fu-snackbar__action {
      color: var(--fu-primary-color);
    }
  }
  
  &--primary {
    background-color: var(--fu-primary-color);
    color: #fff;
    
    .fu-snackbar__action {
      color: #fff;
    }
  }
  
  &--success {
    background-color: var(--fu-success-color);
    color: #fff;
    
    .fu-snackbar__action {
      color: #fff;
    }
  }
  
  &--warning {
    background-color: var(--fu-warning-color);
    color: #fff;
    
    .fu-snackbar__action {
      color: #fff;
    }
  }
  
  &--error {
    background-color: var(--fu-danger-color);
    color: #fff;
    
    .fu-snackbar__action {
      color: #fff;
    }
  }
  
  &--top {
    top: 0;
    animation: fu-slide-down 0.3s;
  }
  
  &--bottom {
    bottom: 0;
    animation: fu-slide-up 0.3s;
  }
  
  &--with-action {
    padding-right: 0;
  }
  
  &__message {
    flex: 1;
    margin-right: 32rpx;
  }
  
  &__action {
    padding: 28rpx 32rpx;
    font-size: 28rpx;
    font-weight: 500;
    white-space: nowrap;
    cursor: pointer;
    
    &:active {
      opacity: 0.7;
    }
  }
}

@keyframes fu-slide-up {
  from {
    transform: translateY(100%);
  }
  to {
    transform: translateY(0);
  }
}

@keyframes fu-slide-down {
  from {
    transform: translateY(-100%);
  }
  to {
    transform: translateY(0);
  }
}
</style> 